//
//  SunUseCardsVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/24.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunUseCardsVC.h"
#import "SunCardsCell.h"

@interface SunUseCardsVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSIndexPath *selectCard;
@property (nonatomic, strong) UITableView *tb;
@property (nonatomic, strong) NSMutableArray *canUse;
@property (nonatomic, strong) NSMutableArray *notUse;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIButton *choseButton;
@property (nonatomic, strong) UIButton *uselessButton;
@property (nonatomic, strong) UILabel *redLine;


@end

@implementation SunUseCardsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的卡券";
    self.canUse = [NSMutableArray array];
    self.notUse = [NSMutableArray array];
    self.dataArr = [NSMutableArray array];
    for (NSDictionary *dic in self.cards) {
        int money = [[dic objectForKey:@"limitMoney"]intValue];
        if (self.days < [[dic objectForKey:@"limitDay"]intValue]){
            [self.notUse addObject:dic];
        }else if (self.money == 0) {
            [self.notUse  addObject:dic];
        }else  if (self.money >= money) {
            [self.canUse addObject:dic];
        }else {
            [self.notUse  addObject:dic];
        }
    }
    self.dataArr = self.canUse;
    
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)creatUI{
    self.tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStyleGrouped];
    [self.view addSubview:self.tb];
    [self.tb lt_All:SunEdgesMake(45, 0, 45, 0) save:YES];
    self.tb.delegate = self;
    self.tb.dataSource = self;
    self.tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tb.showsVerticalScrollIndicator = NO;
    
    self.choseButton = [[UIButton alloc]init];
    [self.view addSubview:self.choseButton];
    [self.choseButton setFrame:CGRectMake(0, 0, SCREENWIDTH/2-1, 45)];
    [self.choseButton setTitle:@"选择优惠券" forState:UIControlStateNormal];
    [self.choseButton setTitleColor:[UIColor colorWithHexString:@"B9B9B9"] forState:UIControlStateNormal];
    [self.choseButton setTitleColor:[UIColor colorWithHexString:@"D0021B"] forState:UIControlStateSelected];
    self.choseButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13];
    self.choseButton.selected = YES;
    [self.choseButton addTarget:self action:@selector(choseAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.redLine = [[UILabel alloc]init];
    [self.view addSubview:self.redLine];
    self.redLine.backgroundColor = [UIColor colorWithHexString:@"D0021B"];
    [self.redLine setFrame:CGRectMake((SCREENWIDTH/2-X(45))/2, 44, X(45), 2)];
    
    UIImageView *line1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invest_line"]];
    [self.view addSubview:line1];
    [line1 setFrame:CGRectMake(SCREENWIDTH/2-1, 18, 1, 20)];
    
    self.uselessButton = [[UIButton alloc]init];
    [self.view addSubview:self.uselessButton];
    [self.uselessButton setFrame:CGRectMake(SCREENWIDTH/2, 0, SCREENWIDTH/2-1, 45)];
    [self.uselessButton setTitle:@"不可用优惠券" forState:UIControlStateNormal];
    [self.uselessButton setTitleColor:[UIColor colorWithHexString:@"B9B9B9"] forState:UIControlStateNormal];
    [self.uselessButton setTitleColor:[UIColor colorWithHexString:@"D0021B"] forState:UIControlStateSelected];
    self.uselessButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13];
    [self.uselessButton addTarget:self action:@selector(uselessAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *noCardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [noCardButton setFrame:CGRectMake(0, SCREENHEIGHT - 45 -64, SCREENWIDTH, 45)];
    [noCardButton setBackgroundColor:WHITE forState:UIControlStateNormal];
    NSString *noCard = @"不使用优惠券";
    [noCardButton setTitle:noCard forState:UIControlStateNormal];
    [noCardButton setTitleColor:[UIColor colorWithHexString:@"030303"] forState:UIControlStateNormal];
    noCardButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [noCardButton addTarget:self action:@selector(doNotUseCard) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:noCardButton];
    
}
- (void)choseAction {
    self.choseButton.selected = YES;
    self.uselessButton.selected = NO;
    [self.redLine setFrame:CGRectMake((SCREENWIDTH/2-X(45))/2, 44, X(45), 2)];
    self.dataArr = self.canUse;
    [self.tb reloadData];
}
- (void)uselessAction{
    self.choseButton.selected = NO;
    self.uselessButton.selected = YES;
    [self.redLine setFrame:CGRectMake(SCREENWIDTH/2+(SCREENWIDTH/2-X(45))/2, 44, X(45), 2)];
    self.dataArr = self.notUse;
    [self.tb reloadData];
}
- (void)doNotUseCard{
    [self.tb deselectRowAtIndexPath:self.selectCard animated:YES];
    if (self.useCardsBlock) {
        self.useCardsBlock(nil);
    }
}

- (void)chooseCards:(UseCardsBlock)block{
    self.useCardsBlock = block;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.choseButton.selected) {
        return 135;
    }else{
        return 195;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SunCardsCell *cell = [SunCardsCell cellWithTableView:tableView];
    if (self.choseButton.selected) {
        [cell useCellData:self.canUse[indexPath.row] isAble:YES isSelect:NO];
    }else{
        [cell useCellData:self.notUse[indexPath.row] isAble:NO isSelect:NO];
        NSDictionary *dic = self.notUse[indexPath.row];
        int money = [[dic objectForKey:@"limitMoney"]intValue];
        if (self.days < [[self.notUse[indexPath.row] objectForKey:@"limitDay"]intValue]){
            cell.reasonLabel.text = [NSString stringWithFormat:@"●投资期限不足%@天",[self.notUse[indexPath.row] objectForKey:@"limitDay"]];
        }else if (self.money == 0) {
            cell.reasonLabel.text = @"●未输入投资金额";
        }else  if (self.money >= money) {
        }else {
            cell.reasonLabel.text = [NSString stringWithFormat:@"●投资金额不足%d元",money];
        }

    }

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectCard = indexPath;
    if (indexPath.section == 0) {
        if (self.useCardsBlock) {
            self.useCardsBlock(self.canUse[indexPath.row]);
        }
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        if (self.useCardsBlock) {
            self.useCardsBlock(nil);
        }
        NSDictionary *dic = self.notUse[indexPath.row];
        int money = [[dic objectForKey:@"limitMoney"]intValue];
        if (self.days < [[self.notUse[indexPath.row] objectForKey:@"limitDay"]intValue]){
            [[Sun shareSun]showMsg:[NSString stringWithFormat:@"投资期限大于%@天方可使用",[self.notUse[indexPath.row] objectForKey:@"limitDay"]]];
        }else if (self.money == 0) {
            [[Sun shareSun]showMsg:@"输入投资金额后方可使用"];
        }else  if (self.money >= money) {
            
        }else {
            [[Sun shareSun]showMsg:[NSString stringWithFormat:@"满%d元方可使用",money]];
        }

    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
